package com.tr.rentcar.controller.houtai.statistics;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.tr.rentcar.model.bean.CarBrand;
import com.tr.rentcar.model.dao.CarBrandDao;
import com.tr.rentcar.model.dao.OrderDao;

/**
 * Servlet 
 */
@WebServlet("/income")
public class InComeTjController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.getRequestDispatcher("/WEB-INF/jsp/houtai_income.jsp").forward(request, response);
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		OrderDao od=new OrderDao();
		Map map=new HashMap();
		Double num;
		PrintWriter out = resp.getWriter();
		Calendar cal = Calendar.getInstance();
		 int now=cal.get(Calendar.YEAR);
		String data=req.getParameter("str");
		String tim=req.getParameter("time");
		String t[]=tim.split("-");
        int year =Integer.parseInt(t[0]);//获取年份
        int month =Integer.parseInt(t[1]);;//获取月份
        int day =Integer.parseInt(t[2]);;//获取日
        //获取最大天数
        String lastDay=getLastDayOfMonth(year, month);
        String last[]=lastDay.split("-");
        int n=Integer.parseInt(last[2]);
        //统计每天
        if(data.equals("day")){
        	map.clear();
        	for(int i=1;i<=n;i++){
            	String time = year + "-" + String.format("%02d", month)+"-" + String.format("%02d", i);
            	try {
            		num=od.getMoney(time);
            		if(num==null){
            			num=0.0;
            		}
            		map.put(String.valueOf(i), num);
    				
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
            }
        	
            out.print(JSONArray.toJSONString(map));
        }
        //统计每月
        if(data.equals("month")){
        	map.clear();
        	for(int i=1;i<=12;i++){
            	String time = year + "-" + String.format("%02d", i);
            	try {
            		num=od.getMoney(time);
            		if(num==null){
            			num=0.0;
            		}
            		map.put(String.valueOf(i), num);
    				
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
            }
            out.print(JSONArray.toJSONString(map));
        }
      //统计每年
        if(data.equals("year")){
        	map.clear();
        	for(int i=2016;i<=now;i++){
            	String time = i+"";
            	try {
            		num=od.getMoney(time);
            		if(num==null){
            			num=0.0;
            		}
            		map.put(String.valueOf(i), num);
    				
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
            }
            out.print(JSONArray.toJSONString(map));
        }
	}
	//获取最大天数
		public String getLastDayOfMonth(int year2,int month2)
	    {
	        Calendar cal = Calendar.getInstance();
	        //设置年份
	        cal.set(Calendar.YEAR,year2);
	        //设置月份
	        cal.set(Calendar.MONTH, month2-1);
	        //获取某月最大天数
	        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	        //设置日历中月份的最大天数
	        cal.set(Calendar.DAY_OF_MONTH, lastDay);
	        //格式化日期
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	        String lastDayOfMonth = sdf.format(cal.getTime());
	         
	        return lastDayOfMonth;
	    }

}
